package fr.cdj.thebox.client.menu.ui;

import java.io.IOException;
import java.util.Arrays;

import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.text.shared.Renderer;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.ValueListBox;
import com.google.inject.Inject;
import com.google.inject.Provider;

import fr.cdj.thebox.client.resources.TheboxBundle;
import fr.cdj.thebox.client.utils.Icon;
import fr.cdj.thebox.shared.FileMetaData.CompareFileType;

public class SortItem extends SimplePanel {

	private ValueListBox<CompareFileType> listBox;
	private Icon button;
	
	@Inject
	public SortItem(TheboxBundle resource, Provider<Icon> iconProvider) {
		this.setStyleName(resource.getStyle().menuItem());

		HorizontalPanel hPanel = new HorizontalPanel();
		hPanel.setSize("100%", "100%");
		hPanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
		
		listBox = new ValueListBox<CompareFileType>(new Renderer<CompareFileType>() {
			@Override
			public String render(CompareFileType object) {
				return object == null ? "" : object.toString();
			}

			@Override
			public void render(CompareFileType object, Appendable appendable) throws IOException {
				appendable.append(object == null ? "" : object.toString());
			}
		});
		
		listBox.setStyleName(resource.getStyle().sortInput());
		listBox.setValue(CompareFileType.NAME, false);
		listBox.setAcceptableValues(Arrays.asList(CompareFileType.values()));
		
		button = iconProvider.get();
		button.addStyleName(resource.getStyle().sort());
		
		hPanel.add(button);
		hPanel.add(listBox);
		
		this.add(hPanel);
	}
	
	public CompareFileType getValue() {
		return listBox.getValue();
	}
	
	public HasClickHandlers getSortClickHandlers() {
		return button;
	}
	
}
